#ifndef _UTILITY_H
#define _UTILITY_H


/* Includes ------------------------------------------------------------------*/
#include "cx32l003_hal.h"
#include "cx32l003_starterkit.h"

/* Private includes ----------------------------------------------------------*/

#include "io_mapping.h"
#include "log.h"
#include "util.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

typedef char           int8;
typedef unsigned char  uint8;


typedef          short int16;
typedef unsigned short uint16;

typedef          int   int32;
typedef unsigned int   uint32;
typedef uint8          SOCKET;

typedef union
{
    uint8_t byte;
    struct
    {
        uint8_t bit0:1;  
        uint8_t bit1:1;  
        uint8_t bit2:1;  
        uint8_t bit3:1;  
        uint8_t bit4:1;  
        uint8_t bit5:1;  
        uint8_t bit6:1;  
        uint8_t bit7:1;  
    }bit;
}union_u8_t;

uint16 atoi16(char* str,uint16 base);             /* Convert a string to integer number */
uint32 atoi32(char* str,uint16 base);             /* Convert a string to integer number */
void itoa(uint16 n,uint8* str, uint8 len);
int validatoi(char* str, int base, int* ret);         /* Verify character string and Convert it to (hexa-)decimal. */
char c2d(uint8_t c);                     /* Convert a character to HEX */

uint16 swaps(uint16 i);
uint32 swapl(uint32 l);

void replacetochar(char * str, char oldchar, char newchar);

void mid(int8* src, int8* s1, int8* s2, int8* sub);
void inet_addr_(unsigned char* addr,unsigned char *ip);


char* inet_ntoa(unsigned long addr);            /* Convert 32bit Address into Dotted Decimal Format */
char* inet_ntoa_pad(unsigned long addr);


uint32 inet_addr(unsigned char* addr);        /* Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address */

char verify_ip_address(char* src, uint8 * ip);/* Verify dotted notation IP address string */

uint16 htons( unsigned short hostshort);    /* htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).*/

uint32 htonl(unsigned long hostlong);        /* htonl function converts a unsigned long from host to TCP/IP network byte order (which is big-endian). */

uint32 ntohs(unsigned short netshort);        /* ntohs function converts a unsigned short from TCP/IP network byte order to host byte order (which is little-endian on Intel processors). */

uint32 ntohl(unsigned long netlong);        /* ntohl function converts a u_long from TCP/IP network order to host byte order (which is little-endian on Intel processors). */

uint16 checksum(unsigned char * src, unsigned int len);        /* Calculate checksum of a stream */

//uint8 check_dest_in_local(u_long destip);            /* Check Destination in local or remote */

uint8 macstr_to_macbyte(uint8* str,uint8 *imac);
unsigned char inet_addr_c(unsigned char* addr,unsigned char *ip);
unsigned char catChar2Hex(unsigned char hByte,unsigned char lByte);
unsigned char Hex_Cat_char(unsigned char Byte, char *str);
#endif
